#include "loginwidget.h"
#include "ui_loginwidget.h"
#include "AbstractLayer/Network/threadmanger.h"
#include <QDebug>
#include <QThread>



LoginWidget::LoginWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowTitle("OneChat");
    isfirst=PageManger::isloginfirst();
    //判断设置库是否为第一次加载
    if(SettingParser::getInstance()->isFirst())
    {
        connect(SettingParser::getInstance(),&SettingParser::s_loadDone,this,&LoginWidget::doSettings);
        ThreadManger::getInstance()->initSetting();
    }
    else
    {
        doSettings();
    }

    //头像加载成功后，跳转界面
    connect(AvatarLib::getInstance(),&AvatarLib::AvatarloadDone,this,&LoginWidget::jumpWidget);
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

//加载设置到界面
void LoginWidget::doSettings()
{
    qDebug()<<SettingParser::getInstance()->getValue("remember").toInt();
    ui->checkBox_remember->setChecked(SettingParser::getInstance()->getValue("remember").toInt());
    ui->checkBox_autologin->setChecked(SettingParser::getInstance()->getValue("autologin").toInt());
    ui->lineEdit_user->setText(SettingParser::getInstance()->getValue("user"));
    if(SettingParser::getInstance()->getValue("remember").toInt())
    {
        ui->lineEdit_password->setText(SettingParser::getInstance()->getValue("password"));
        if(SettingParser::getInstance()->getValue("autologin").toInt()&&isfirst)
        {
            on_btn_login_clicked();
        }
    }
}

//跳转窗口
void LoginWidget::jumpWidget()
{
    disconnect(AvatarLib::getInstance(),&AvatarLib::AvatarloadDone,this,&LoginWidget::jumpWidget);
    if(SettingParser::getInstance()->getValue("remember").toInt())
    {
        SettingParser::getInstance()->setValue("user",ui->lineEdit_user->text());
        SettingParser::getInstance()->setValue("password",ui->lineEdit_password->text());
    }
    connect(JSHandler::getInstance(),&JSHandler::s_logInPass,this,&LoginWidget::logPass);
    connect(JSHandler::getInstance(),&JSHandler::s_logInError,this,&LoginWidget::logError);
    connect(JSHandler::getInstance(),&JSHandler::s_logInBan,this,&LoginWidget::logBan);
    PageManger::switchToWidget(PageManger::MAINWIDGET);
}

/*关闭窗口按钮的槽函数
 * 返回值：无
 */
void LoginWidget::on_btn_close_clicked()
{
    this->close();
}

/*最小化窗口的槽函数
 * 返回值：无
 */
void LoginWidget::on_btn_min_clicked()
{
    this->setWindowState(Qt::WindowMinimized);
}

/*登录窗口的槽函数
 * 返回值：无
 */
void LoginWidget::on_btn_login_clicked()
{
    ThreadManger::getInstance()->sendVerify(ui->lineEdit_user->text(),ui->lineEdit_password->text());
    connect(JSHandler::getInstance(),&JSHandler::s_logInPass,this,&LoginWidget::logPass);
    connect(JSHandler::getInstance(),&JSHandler::s_logInError,this,&LoginWidget::logError);
    connect(JSHandler::getInstance(),&JSHandler::s_logInBan,this,&LoginWidget::logBan);

    //切换到主窗口
    //disconnect(thread_manger,this);
}


//以下为拖拽窗口代码
void LoginWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        if(event->y()<50){
        m_bDrag = true;
        //获得鼠标的初始位置
        mouseStartPoint = event->globalPos();
        //获得窗口的初始位置
        windowTopLeftPoint = this->frameGeometry().topLeft();
        }
    }
}

void LoginWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bDrag)
    {
        //获得鼠标移动的距离
        QPoint distance = event->globalPos() - mouseStartPoint;
        //改变窗口的位置
        this->move(windowTopLeftPoint + distance);
    }
}

void LoginWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bDrag = false;
    }
}


void LoginWidget::on_checkBox_remember_stateChanged(int arg1)
{
    SettingParser::getInstance()->setValue("remember",QString::number(arg1));
}

void LoginWidget::on_checkBox_autologin_stateChanged(int arg1)
{
    SettingParser::getInstance()->setValue("autologin",QString::number(arg1));
}

void LoginWidget::on_btn_signup_clicked()
{
    PageManger::switchToWidget(PageManger::SIGNINWIDGET);
}

//创建用户空间
void LoginWidget::creatNewUserFile(const QString& username)
{
    //ThreadManger::getInstance()->createUserFile();

    //连接到数据库
    //创建完文件夹后，开始加载头像
    connect(ThreadManger::getInstance(),&ThreadManger::s_transCreateDirDone,ThreadManger::getInstance(),&ThreadManger::sendGetContactList);
    //开始创建新数据库
    ThreadManger::getInstance()->createNewDatabase(username);
    ThreadManger::getInstance()->initDatabase();
    connect(ContactLib::getInstance(),&ContactLib::s_friendLibLoadDone,ThreadManger::getInstance(),&ThreadManger::loadAvatarFromFile);
    qDebug()<<"creatNewUserFile";
}

void LoginWidget::logPass()
{
    useroc=ui->lineEdit_user->text();
    MyJson::getInstance()->myJsonInit(useroc);
    ThreadManger::getInstance()->initFileLocal(useroc);
    //设置数据库的用户oc
    ThreadManger::getInstance()->setDBUseroc(useroc);
    //设置消息的用户oc
    ThreadManger::getInstance()->setMesCurrentOC(useroc);
    //设置接收文件的用户oc
    ThreadManger::getInstance()->initRecvFile(useroc);
    //如果该用户本地不存在
    if(!QDir().exists("./User/"+useroc+"/"))
    {
        //创建用户头像文件夹，为了存放随后接收的用户头像
        ThreadManger::getInstance()->createNewUserAvatar();
        //接收到用户信息后，创建用户的文件夹
        connect(CustomHandler::getInstance(),&CustomHandler::s_recvUserInfo,this,&LoginWidget::creatNewUserFile);
        //发送获取用户信息请求
        ThreadManger::getInstance()->sendGetUserInfo(useroc);
    }
    else
    {
        ThreadManger::getInstance()->initDatabase();
        //加载头像
        ThreadManger::getInstance()->loadAvatarFromFile();
    }

    ui->label_error->setText("登录成功");
}

void LoginWidget::logError()
{
    ui->label_error->setText("账号或密码错误");
    ui->lineEdit_password->clear();
    ui->lineEdit_user->clear();
}

void LoginWidget::logBan()
{
    ui->label_error->setText("你已被禁止登录");
}


